﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyWords
{
    public partial class Marked : Form
    {
        CurrentUser user;
        Word currentWord;
        public Marked(CurrentUser user)
        {
            this.user = user;
            InitializeComponent();
        }

        private void Marked_Load(object sender, EventArgs e)
        {
            SQLiteDataReader reader = CurrentUser.sql.ReadFullTable("myList");
            CurrentUser.myList.Clear();
            //currentWord = new Word();
            // 加载allWords表中全部的单词到listbox
            while (reader.Read())
            {
                string userID = reader.GetString(reader.GetOrdinal("userID"));
                if(userID.Equals(CurrentUser.ID))
                {
                    string wordID = reader.GetString(reader.GetOrdinal("wordID"));
                    string word = reader.GetString(reader.GetOrdinal("word"));
                    string mean = reader.GetString(reader.GetOrdinal("mean"));
                    string prop = reader.GetString(reader.GetOrdinal("prop"));
                    CurrentUser.myList.Add(new Word(word,mean,prop,wordID,1));
                }
                
            }

            listbox_wordList.Items.Clear();
            for (int i = 0; i < CurrentUser.myList.Count; i++)
            {
                listbox_wordList.Items.Add(CurrentUser.myList[i].word);
            }
            reader = CurrentUser.sql.ReadFullTable("myList");
            if(listbox_wordList.Items.Count != 0)listbox_wordList.SelectedIndex = 0;


            for(int i = 0; i < CurrentUser.myList.Count; i++)
            {
                // 初始化显示
                currentWord = CurrentUser.myList[i];
                text_word.Text = currentWord.word;
                text_meaning.Text = currentWord.mean;
                text_prop.Text = currentWord.prop;
                if (currentWord.isM == 1) text_isM.Text = "*";
                else text_isM.Text = "";
            }


            
        }

        private void listbox_wordList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 重新选择单词时，更新界面显示
            currentWord = CurrentUser.myList[listbox_wordList.SelectedIndex];
            text_word.Text = currentWord.word;
            text_meaning.Text = currentWord.mean;
            text_prop.Text = currentWord.prop;
            if (currentWord.isM == 1) text_isM.Text = "*";
            else text_isM.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            currentWord.doMark(); // 调用单词的添加/移除标记功能
            listbox_wordList.Items.Clear(); // 清空当前列表

            // 移除myList中的该单词
            for (int i = CurrentUser.myList.Count - 1; i >= 0; i--)
            {
                if(CurrentUser.myList[i].wordID.Equals(currentWord.wordID))
                {
                    CurrentUser.myList.Remove(CurrentUser.myList[i]);
                 }
            }

            // 重新从myList中加载生词表
            for (int i = 0; i < CurrentUser.myList.Count; i++)
            {
                listbox_wordList.Items.Add(CurrentUser.myList[i].word);
            }
            text_word.Text = "";
            text_meaning.Text = "";
            text_prop.Text = "";
            text_isM.Text = "";


        }
        //下一个单词
        private void button1_Click(object sender, EventArgs e)
        {
            //防止溢出
            if(listbox_wordList.SelectedIndex+1 < listbox_wordList.Items.Count)
                listbox_wordList.SelectedIndex++;
        }
        // 上一个单词
        private void button2_Click(object sender, EventArgs e)
        {
            //防止溢出
            if (listbox_wordList.SelectedIndex > 0)
                listbox_wordList.SelectedIndex--;
        }
    }
}
